package com.hz.basic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hz.basic.entity.CbExpandBrowseLogEntity;
import com.hz.basic.entity.CbExpandResourceEntity;
import com.hz.basic.service.CbExpandBrowseLogService;
import com.hz.common.utils.PageUtils;
import com.hz.common.utils.ResponseData;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.FutureTask;

@RestController
@RequestMapping("expandresourcelog")
@Api(tags="扩展资源日志")
public class ExpandResourceLogController {

    @Autowired
    private CbExpandBrowseLogService cbExpandBrowseLogService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @ApiOperation("存储扩展版本日志记录")
    @GetMapping("/savelog")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "扩展版本类型，0：碑帖欣赏1：国学经典2：书法知识", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "addType", value = "所属类别，0：软笔1：硬笔", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "rid", value = "扩展资源id", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String",required = true)
    })
    public ResponseData<String> savelog(@RequestParam("cid") Long cid,@RequestParam("addType") int addType,@RequestParam("rid") Long rid,@RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        if ((cid == 0 || cid == 1 || cid == 2) && (addType == 0 || addType == 1)){
            /*CbExpandBrowseLogEntity expandResourceLog = cbExpandBrowseLogService.getOne(new QueryWrapper<CbExpandBrowseLogEntity>().eq("rid", rid).eq("tid",userId).eq("add_type",addType).eq("expand_edition_type",cid));
            if (expandResourceLog != null){
                return ResponseData.success("该日志记录已存储，无需重复存储！");
            }*/
            //写入日志
            CbExpandBrowseLogEntity expandResourceLog = new CbExpandBrowseLogEntity();
            expandResourceLog.setTid(userId);
            expandResourceLog.setAddType(addType);
            expandResourceLog.setCreatetime(new Date());
            expandResourceLog.setExpandEditionType(cid.intValue());
            expandResourceLog.setRid(rid);
            cbExpandBrowseLogService.save(expandResourceLog);
            return ResponseData.success("添加日志记录成功！");
        }
        return ResponseData.success("字段不符合要求！");
    }

    @ApiOperation("获取扩展版本日志记录")
    @GetMapping("/getlog")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "扩展版本类型，0：碑帖欣赏1：国学经典2：书法知识", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "addType", value = "所属类别，0：软笔1：硬笔", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String",required = true),
            @ApiImplicitParam(name = "condition", value = "查询条件", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "curpage", value = "分页，当前页", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "numpage", value = "分页，页数量", paramType = "query", dataType = "int", required = true)
    })
    public ResponseData<PageUtils<List<CbExpandResourceEntity>>> getlog(@RequestParam("cid") Long cid,@RequestParam("addType") int addType,@RequestHeader("Authorization") String token,@RequestParam(name = "condition",required = false) String condition
            ,@RequestParam(name = "curpage") int curpage,@RequestParam(name = "numpage") int numpage){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        if ((cid == 0 || cid == 1 || cid == 2) && (addType == 0 || addType == 1)){
            PageUtils<List<CbExpandResourceEntity>> page = cbExpandBrowseLogService.getlog(userId,addType,cid,condition,curpage,numpage);
            return ResponseData.success(page);
        }
        return ResponseData.success("字段不符合要求！");
    }

    @ApiOperation("删除扩展版本日志记录")
    @GetMapping("/deletelog")
    @ApiImplicitParam(name = "id", value = "日志id", paramType = "query", dataType = "Long", required = true)
    public ResponseData<Bool> deletelog(@RequestParam("id") Long id){
        return ResponseData.success(cbExpandBrowseLogService.removeById(id));
    }

}
